﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace xKnight.WebCrawling.Core.HTTP
{
    public class HttpRequestBuilder
    {
        private HttpWebRequest _httpWebRequest;
        private string _httpMethod=null;
        private int _timeout=20000;
        private string _contentType=null;
        private string _userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
        private bool _autoRedirect = false;
        private bool _keepAlive = false;
        private byte[] _data = new byte[0];
        private string _url=null;
        private bool _cookieAware;
        private CookieCollection _cookies;

        public HttpRequestBuilder SetHttpMethod(string httpMethod)
        {
            _httpMethod = httpMethod;
            return this;
        }

        public HttpRequestBuilder SetTimeout(int timeout)
        {
            _timeout = timeout;
            return this;
        }

        public HttpRequestBuilder SetContentType(string contentType)
        {
            _contentType = contentType;
            return this;
        }

        public HttpRequestBuilder SetUserAgent(string userAgent)
        {
            _userAgent = userAgent;
            return this;
        }

        public HttpRequestBuilder SetAllowAutoRedirect(bool autoRedirect)
        {
            _autoRedirect = autoRedirect;
            return this;
        }

        public HttpRequestBuilder SetKeepAlive(bool keepAlive)
        {
            _keepAlive = keepAlive;
            return this;
        }

        public HttpRequestBuilder SetUrl(string url)
        {
            _url = url;
            return this;
        }

        public HttpRequestBuilder SetData(byte[] data)
        {
            _data = data;
            return this;
        }

        public HttpRequestBuilder SetCookieAware(bool cookieAware)
        {
            _cookieAware = cookieAware;
            return this;
        }

        public HttpRequestBuilder SetCookies(CookieCollection cookies)
        {
            _cookies = cookies;
            return this;
        }

        public HttpRequestBuilder SetCookies(Cookie[] cookies)
        {
            if (cookies == null)
            {
                _cookies = null;
            }
            else
            {

                _cookies = new CookieCollection();

                for (int i = 0; i < cookies.Length; i++)
                    _cookies.Add(cookies[i]);
            }

            return this;
        }

        public HttpWebRequest getHttpWebRequest()
        {
            _httpWebRequest = WebRequest.Create(_url) as HttpWebRequest;
            _httpWebRequest.Method = _httpMethod;
            _httpWebRequest.KeepAlive = _keepAlive;
            _httpWebRequest.Timeout = _timeout;
            _httpWebRequest.AllowAutoRedirect = _autoRedirect;
            _httpWebRequest.ContentType = _contentType;
            _httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            if (_cookieAware)
                _httpWebRequest.CookieContainer = new CookieContainer();

            if (_cookies != null)
            {
                if(_httpWebRequest.CookieContainer==null)
                    _httpWebRequest.CookieContainer = new CookieContainer();

                for(int i=0;i<_cookies.Count;i++)
                    _httpWebRequest.CookieContainer.Add(_cookies[i]);
            }

            _httpWebRequest.Headers.Add("Accept-Language: en-US");
            _httpWebRequest.Headers.Add("Accept-Encoding: gzip, deflate");

            if (_httpWebRequest.Method.ToLower() == "post")
            {
                _httpWebRequest.ContentLength = _data.Length;

                using (Stream stream = _httpWebRequest.GetRequestStream())
                    stream.Write(_data, 0, _data.Length);
            }

            return _httpWebRequest;
        }


    }
}
